package com.lyf.emall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyf.emall.bean.*;
import com.lyf.emall.mapper.AdminMapper;
import com.lyf.emall.mapper.RoleMapper;
import com.lyf.emall.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author Lfy
 * @Date 2022/4/12-15:05
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    RoleMapper roleMapper;

    @Override
    public ResponseResult getAllAdmin(Page page) {
        if (page.getPageNo() <= 0){
            return new ResponseResult(400, "页码不能小于等于0");
        }

        List<Admin> admins = adminMapper.queryAllAdmin();
        PageHelper.startPage(page.getPageNo(), page.getPageNum());

        PageInfo<Admin> pageInfo = new PageInfo<>(admins);
        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "获得管理员列表成功", map);
    }

    @Override
    public ResponseResult isAdmin(String userName) {
        if (!StringUtils.hasText(userName)){
            return new ResponseResult(400, "用户名不能为空");
        }
        Admin admin = adminMapper.queryAdminByUserName(userName);
        if (admin == null){
            return new ResponseResult(200, "用户名不是重名");
        }
        return new ResponseResult(400, "用户名重名");
    }

    @Transactional
    @Override
    public ResponseResult addAdmin(Admin admin, Integer roleId) {
        Integer insertAdmin = adminMapper.insertAdmin(admin);
        int insertRoleId = roleMapper.insertRoleId(admin.getUserId(), roleId);

        if (insertAdmin <= 0 || insertRoleId <= 0){
            return new ResponseResult(400, "添加管理员失败");
        }
        return new ResponseResult(200, "添加管理员成功");
    }

    @Override
    public ResponseResult updatePassword(SysUser sysUser) {
        if (!StringUtils.hasText(sysUser.getPassword())){
            return new ResponseResult(400, "密码不能为空");
        }
        Integer updateAdminPassword = adminMapper.updateAdminPassword(sysUser);
        if (updateAdminPassword <= 0){
            return new ResponseResult(400, "修改密码失败");
        }
        return new ResponseResult(200, "修改密码成功");
    }

    @Transactional
    @Override
    public ResponseResult updateAdmin(SysUser sysUser, Integer roleId) {
        Integer updateAdminInfo = adminMapper.updateAdminInfo(sysUser);
        Integer updateUserRole = roleMapper.updateUserRole(new SysUserRole(sysUser.getUserId(), roleId));
        if (updateAdminInfo <= 0 || updateAdminInfo <= 0){
            return new ResponseResult(400, "修改管理员信息失败");
        }
        return new ResponseResult(200, "修改管理员信息成功");
    }

    @Override
    public ResponseResult updateStatus(Integer userId, Integer status) {
        Integer updateAdminStatus = adminMapper.updateAdminStatus(userId, status);
        if (updateAdminStatus <= 0){
            return new ResponseResult(400, "修改管理员状态失败");
        }
        return new ResponseResult(200, "修改用户状态成功");
    }

    @Override
    public ResponseResult deleteAdmin(Integer userId) {
        Integer deleteAdmin = adminMapper.deleteAdmin(userId);
        if (deleteAdmin <= 0){
            return new ResponseResult(400, " 删除用户失败");
        }
        return new ResponseResult(200, "删除用户成功");
    }
}
